import maya.cmds as cmds
import maya.mel as mel


def GUI():
	IsoCam()

class IsoCam():
	def __init__(self):
		self.GUI = {}
		self.panel = cmds.getPanel( wf=True ) #active panel
		try:
			self.cam = cmds.modelPanel( self.panel, q=True, camera = True ) #current camera
		except RuntimeError:
			self.cam = False #if we cannot find a camera then quit out of the script
		if self.cam:
			self.GUI['window']	 = cmds.window( title = 'Selection Isolated.')
			self.GUI['layout1'] = cmds.columnLayout()
			self.GUI['button1'] = cmds.button(l='Leave Isolation', h=40, w=250,	c=self._close)
			cmds.setParent('..')
			if cmds.ls( sl = True):
				self.newcam = cmds.duplicate( self.cam )[0] #create new camera based off old camera
				cmds.lookThru( self.panel, self.newcam ) #set new camera to panel
				cmds.showWindow( self.GUI['window'] )
				mel.eval('enableIsolateSelect %s %i' % (self.panel,True) )
			else:
				cmds.confirmDialog( message = 'You need to select something to isolate it.' )

	def _close(self, *args): #close window when button pressed
		if cmds.window( self.GUI['window'], ex=True ):
			cmds.deleteUI( self.GUI['window'] )

	def __del__(self): #clean up camera and turn off isolate
		if self.cam:
			if cmds.objExists( self.newcam ):
				cmds.lookThru( self.panel, self.cam )
				cmds.delete( self.newcam )#remove camera
				cmds.isolateSelect( self.panel, state = False )


